package com.wb.oldwomen.module;

import android.util.Log;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.Util;
import com.wb.fw.WorldCamera;
import com.wb.fw.WorldObject;
import com.wb.oldwomen.GamePlayLayer;
import com.wb.oldwomen.Ioldwomam;
import com.wb.oldwomen.R;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.ease.EaseOut;
import com.wiyun.engine.actions.ease.EaseSineIn;
import com.wiyun.engine.actions.ease.EaseSineOut;
import com.wiyun.engine.actions.JumpBy;
import com.wiyun.engine.actions.JumpTo;
import com.wiyun.engine.actions.MoveBy;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.afcanim.MWSprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;

public class Oldwoman extends WorldObject implements Ioldwomam {
	public static Oldwoman make(GamePlayLayer parent) {
		return new Oldwoman(parent);
	}

	protected Oldwoman(GamePlayLayer parent) {
		Texture2D jump = Texture2D.makePNG(R.drawable.women_jump);
		Texture2D run = Texture2D.makePNG(R.drawable.women_run);
		Texture2D gougou = Texture2D.makePNG(R.drawable.gougou);
		jump.autoRelease();
		run.autoRelease();
		gougou.autoRelease();
		msprite = MWSprite.make(R.raw.womem, WOMAN_ANIM_BEGIN, run, jump,
				gougou);
		// msprite.setDebugDrawFrameRect(true);
		msprite.setDebugDrawCollisionRect(true);
		msprite.autoRelease();
		msprite.setScale(Global.sScaleRate);
		msprite.setUnitInterval(0.06f);

		mparent = parent;
		mparent.addChild(msprite, Z_ROAD);

	}

	GamePlayLayer mparent;
	private int mstate;
	MWSprite msprite;
	int mbtnState, manimtype;

	public void setLevelData(int level) {
		manimtype = Util.getAnimType(GateActivity.sInstance);//ANIM_TYPE_GOUGOU;
		switchState(STATE_BEGIN);
		mposx = WOMAN_FOCUS_X;
		mposy = Y_ROAD_RUN;
		mspeedx = NORMAL_X_SPEED;
		msprite.setPosition(mposx * Global.sScaleRate, mposy
				* Global.sScaleRate);
		// menableMove = true;

	}

	public void clearLevelData() {
		mcollisionObj = null;
		mstopCollisionObj = null;
	}

	public void startRun() {
		switchState(STATE_RUN);
	}

	public int getAppleCount() {
		return 0;
	}

	void switchState(int state) {
		switch (state) {
		case STATE_BEGIN:
			msprite.setLoopCount(-1);
			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_BEGIN));
			break;
		case STATE_HURT:
			msprite.setRotation(180);
			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_STOP));
			break;
//		case STATE_JUMP_DOWN:// 落地瞬间
//			mspeedx = NORMAL_X_SPEED;
//			msprite.setLoopCount(1);
//			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_JUMP_DOWN));
//			break;
		case STATE_JUMPING:// 下落状态
			jumpdown();
			break;
		case STATE_JUMP_UP:// 上升状态
			mspeedx = JUMP_X_SPEED;
			float height = WOMAM_MAX_JUMP_HEIGHT * Global.sScaleRate;
			float duration = WOMAM_MAX_JUMP_HEIGHT / WOMAN_JUMP_Y_SPEEDR_PER_SEC;

			MoveBy mb = MoveBy.make(duration, 0, height);
			mb.autoRelease();
			mb.setCallback(new Callback() {
				@Override
				public void onUpdate(int arg0, float arg1) {
				}

				@Override
				public void onStop(int arg0) {
					switchState(STATE_JUMPING);
				}

				@Override
				public void onStart(int arg0) {
				}
			});
			EaseSineOut eso = EaseSineOut.make(mb);
			eso.autoRelease();
			msprite.runAction(eso);
			msprite.setLoopCount(1);
			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_JUMP_ON));
			break;
		case STATE_RUN:
			mspeedx = NORMAL_X_SPEED;
			mangle = 0;
			msprite.setRotation(mangle);
			msprite.stopAllActions();
			msprite.setLoopCount(-1);
			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_RUN));
			break;
		case STATE_STOP:
			mspeedx = 0;
			msprite.setLoopCount(1);
			msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_STOP));
			mparent.showResult();
			break;
		}
		Log.d("xltest", "switch state=" + state + ",last state=" + mstate);
		mstate = state;
	}

	private void jumpdown() {
		mspeedx = JUMP_X_SPEED;
		msprite.stopAllActions();
		float duration = (msprite.getPositionY() - Y_ROAD_RUN)
				/ WOMAN_JUMP_Y_SPEEDR_PER_SEC;
		float height = -(msprite.getPositionY() - Y_ROAD_RUN)
				* Global.sScaleRate;
//			Log.d("xltest", "duration=" + duration + ",height=" + height
//					+ ",msprite.getPositionY()=" + msprite.getPositionY()
//					+ ",Y_ROAD_RUN=" + Y_ROAD_RUN);

		MoveBy mb = MoveBy.make(duration, 0, height);
		mb.autoRelease();
		mb.setCallback(new Callback() {
			@Override
			public void onUpdate(int arg0, float arg1) {
			}

			@Override
			public void onStop(int arg0) {
				switchState(STATE_RUN);
			}

			@Override
			public void onStart(int arg0) {
			}
		});
		EaseSineIn esi = EaseSineIn.make(mb);
		esi.autoRelease();
		msprite.runAction(esi);
		// }
		msprite.setLoopCount(1);
		msprite.playAnimation(getAnimBaseType(WOMAN_ANIM_JUMPING));
	}

	int getAnimBaseType(int anim) {
		int result = anim;
		if (manimtype == ANIM_TYPE_GOUGOU) {
			result += WOMAN_ANIM_OFFSET;
		}
		return result;
	}

	public void btnDown() {
		mbtnState = BTN_STATE_DOWN;
		switch (mstate) {
		case STATE_RUN:
			switchState(STATE_JUMP_UP);
			break;
		}
	}

	public void btnUp() {
		mbtnState = BTN_STATE_UP;
		switch (mstate) {
		case STATE_JUMP_UP:
			// switchState(STATE_JUMPING);
			msprite.stopAllActions();
			break;
		}
	}

	float mangle;

	public void update(float delta) {
		if (mcollisionObj != null) {
			int type = mcollisionObj.getType();
			if (type == OBJ_TYPE_XIEPO) {
				if (!mcollisionObj.checkMWCollision(getScreenCollisionRect())) {
					mcollisionObj = null;
					switchState(STATE_JUMPING);
					if (mparent.getCurLevel() == 0)
						HitControl.getInstance(mparent).setHitVisible(
								false,
								HIT_FOOT_FIRST,null);
				}
			} else if (type == OBJ_TYPE_ONE_BOX || type == OBJ_TYPE_TWO_BOX){
				if (!mcollisionObj.checkCollision(getScreenCollisionRect())) {
					mcollisionObj = null;
					switchState(STATE_JUMPING);
					if (mparent.getCurLevel() == 0)
					HitControl.getInstance(mparent).setHitVisible(
							false,
							HIT_FOOT_FIRST,null);
				}
			}
		}
		
		switch (mstate) {
		case STATE_RUN:
			if (mstopCollisionObj == null) {
				mposx += mspeedx;
			}
			break;
		case STATE_STOP:
			break;
		case STATE_JUMPING:
			mposx += mspeedx;
			if (mcollisionObj != null) {
				switchState(STATE_RUN);
				if (mparent.getCurLevel() == 0)
					HitControl.getInstance(mparent).setHitVisible(
							true,
							HIT_FOOT_FIRST,
							WYPoint.make(Global.sWYSize.width / 2,
									Global.sWYSize.height / 2));
			}
			break;
		case STATE_JUMP_UP:
			if (mstopCollisionObj == null) {
				mposx += mspeedx;
			} else {
				if (mstopCollisionObj.getType() == OBJ_TYPE_XIEPO) {
					if (!mstopCollisionObj.checkLeftWMCollision(getScreenCollisionRect())) {
						mposx += mspeedx;
						mstopCollisionObj = null;
						if (mparent.getCurLevel() == 0)
							HitControl.getInstance(mparent).setHitVisible(false,
								HIT_HOLD, null);
					}
				} else {
					if (!mstopCollisionObj.checkLeftCollision(getScreenCollisionRect())) {
						mposx += mspeedx;
						mstopCollisionObj = null;
						if (mparent.getCurLevel() == 0)
							HitControl.getInstance(mparent).setHitVisible(false,
								HIT_HOLD, null);
					}
				}
			}
			break;
		default:
			mposx += mspeedx;
			break;
		}
		msprite.tick(delta);
		computerWorldCollisionRect();
	}

	void computerWorldCollisionRect() {
		WYRect rect = msprite.getCollisionRect(0);
		if (rect != null) {
			// Log.d("xltest",
			// "collision rect="+rect.minX()+",my="+rect.minY()+",ax="+rect.maxX()+",ay="+rect.maxY());
			mcollisionRect = WYRect.make(msprite.getPositionX() + rect.minX(),
					msprite.getPositionY() + rect.minY(),
					rect.maxX() - rect.minX(), rect.maxY() - rect.minY());
			// Log.d("xltest",
			// "-------------collision rect="+rect.minX()+",my="+rect.minY()+",ax="+rect.maxX()+",ay="+rect.maxY());
		} else {
			mcollisionRect = null;
		}
	}

	WYRect mcollisionRect;

	/**
	 * 屏幕坐标的框。
	 * 
	 * @return
	 */
	public WYRect getScreenCollisionRect() {
		return mcollisionRect;
	}

	Obj mstopCollisionObj, mcollisionObj;

	public void setLeftCollisionObj(Obj obj) {
//		if (mstopCollisionObj != null && mstopCollisionObj == obj)
//			return;
		mstopCollisionObj = obj;
		if (mparent.getCurLevel() == 0)
			HitControl.getInstance(mparent).setHitVisible(
				true,
				HIT_HOLD,
				WYPoint.make(Global.sEndX - 260 * Global.sScaleRate,
						Global.sEndY + 73 * Global.sScaleRate));
//		if (mstate == STATE_JUMP_UP) {
//			switchState(STATE_JUMPING);
//		}
//		else 
//		if (mstate != STATE_RUN && mstate != STATE_JUMPING) {
//			switchState(STATE_RUN);
//		}
	}
	public void setCollisionObj(Obj obj) {
		mcollisionObj = obj;
//		if (mstate != STATE_RUN) {
//			switchState(STATE_RUN);
//		}
	}
	// public void enableMove(Obj obj, boolean enable) {
	// if (enable) {//设置可以动的条件。配置对象不为空，且传入对象和配置对象是同一个。
	// if (mcollisionObj != null && obj == mcollisionObj) {
	// mcollisionObj = null;
	// menableMove = true;
	// }
	// } else {
	// menableMove = enable;
	// mcollisionObj = obj;
	// }
	// }
	// //////////////
}
